/* $Id:culture.cc $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#define DEBUG_ROUTINE "culture"

#include <sstream>
#include <fstream>
#include "../general/util.h"
#include "../general/log.h"
#include "../general/channel.h"
#include "culture.h"
#include "../general/general.h"
#include "unit.h"

culture::culture() {
    name="";
    description="";
    guilds=map<string,class guild>();
}

// Field : name

const string culture::getName() const { return name; }
void culture::setName(const string v) { name=v; }

// Field : description

const string culture::getDescription() const { return description; }
void culture::setDescription(const string v) { description=v; }

// Field : guilds

class guild& culture::getGuilds(const string v) { return guilds[v]; }
map<string,class guild>& culture::listGuilds() { return guilds; }
void culture::setGuilds(class guild &v) {
    if (v.getName()=="") throw unnamed();
    guilds[v.getName()]=v;
}
void culture::delGuilds(const string v) { guilds.erase(v); }
const bool culture::isGuilds(const string v) const { return guilds.count(v)>0; }

void culture::save(class channel &ch) {
    ch.write("culture", "");
    ch.write("n", getName());
    ch.write("d", getDescription());
    loopCultureGuilds(this, s_guilds) {
        s_guilds->save(ch);
    }
}

void culture::load(class channel &ch) {
    if (ch.key=="n" || ch.key=="name") { setName(ch.value); ch.next(); }
    if (ch.key=="d" || ch.key=="description") { setDescription(ch.value); ch.next(); }
    while (ch.key=="guild" || ch.key=="guilds" || ch.key=="unit") {
        if (ch.key=="unit") {
            unit_repo.load(ch);
        } else {
            ch.next();
            class guild rec;
            rec.load(ch);
            setGuilds(rec);
        }
    }
}

void culture::xml(ofstream &f, int l, int r) {
    f << ncpy(l, "  ") << "<culture name=\"" << xml_encode(getName()) <<
    "\" description=\"" << xml_encode(getDescription()) <<
    "\">" << endl;
    if (r==0) {
        loopCultureGuilds(this, s_guilds) {
            s_guilds->xml(f,l+1);
        }
    }
    f << ncpy(l, "  ") << "</culture>" << endl;
}

// Repository

cultureRepository culture_repo;

class culture& cultureRepository::getCulture(const string v) { return cultures[v]; }
void cultureRepository::setCulture(class culture& v) {
    if (v.getName()=="") throw unnamed();
    cultures[v.getName()]=v;
}
map<string,class culture>& cultureRepository::listCulture() { return cultures; }
void cultureRepository::delCulture(const string v) { cultures.erase(v); }
const bool cultureRepository::isCulture(const string v) const { return cultures.count(v)>0; }
void cultureRepository::save(class channel &ch) {
    loopCultures(rec) {
        rec->save(ch);
    }
}

bool cultureRepository::load(class channel &ch) {
    if (ch.key=="culture") {
        ch.next();
        class culture rec;
        rec.load(ch);
        setCulture(rec);
        return true;
    }
    return false;
}
